<?php

include_once('EchoUtil.php');
class RegxUtil {

    public static function regxPhone($code, $value) {
        if (!isset($_POST[$value])) {// is null
            echo EchoUtil::msgError($code, 'param error : '.$value.' is null !');
            exit;
        }
        if (empty($_POST[$value])) {
            echo EchoUtil::msgError($code, 'param error : '.$value.' is empty !');
            exit;
        }
        $len = strlen($_POST[$value]);
        if ($len != 11) {// is not 11 bit count
            echo EchoUtil::msgError($code, 'param error : '.$value.' length is '.$len.' not 11 !');
            exit;
        }
        if(!preg_match("/^1[3|4|5|7|8][0-9]{9}$/", $_POST[$value])){// is not right format
            echo EchoUtil::msgError($code, 'param error : '.$value.' format is not right !');
            exit;
        }
        return $_POST[$value];
    }

    public static function regxUsername($code, $value) {
        if (!isset($_POST[$value])) {// is null
            echo EchoUtil::msgError($code, 'param error : '.$value.' is null !');
            exit;
        }
        if (empty($_POST[$value])) {
            echo EchoUtil::msgError($code, 'param error : '.$value.' is empty !');
            exit;
        }
        $len = strlen($_POST[$value]);
        if ($len < 2 || $len > 15) {// is less than 2 or more than 20
            echo EchoUtil::msgError($code, 'param error : '.$value.' length is '.$len.' less than 2 or more than 20 !');
            exit;
        }
        if(preg_match("/\/|\~|\!|\@|\#|\\$|\%|\^|\&|\*|\(|\)|\_|\+|\{|\}|\:|\<|\>|\?|\[|\]|\,|\.|\/|\;|\'|\`|\-|\=|\\\|\|$/", $_POST[$value])){// is not right format
            echo EchoUtil::msgError($code, 'param error : '.$value.' format is not right !');
            exit;
        }
        return $_POST[$value];
    }

    public static function regxPassword($code, $value) {
        if (!isset($_POST[$value])) {// is null
            echo EchoUtil::msgError($code, 'param error : '.$value.' is null !');
            exit;
        }
        if (empty($_POST[$value])) {
            echo EchoUtil::msgError($code, 'param error : '.$value.' is empty !');
            exit;
        }
        $len = strlen($_POST[$value]);
        if ($len != 32) {// is not 32 standard bit count
            echo EchoUtil::msgError($code, 'param error : '.$value.' length is '.$len.' not 32 !');
            exit;
        }
        return $_POST[$value];
    }

    public static function regxVerifycode($code, $value) {
        if (!isset($_POST[$value])) {// is null
            echo EchoUtil::msgError($code, 'param error : '.$value.' is null !');
            exit;
        }
        if (empty($_POST[$value])) {
            echo EchoUtil::msgError($code, 'param error : '.$value.' is empty !');
            exit;
        }
        $len = strlen($_POST[$value]);
        if ($len != 6) {// is not 6 bit count
            echo EchoUtil::msgError($code, 'param error : '.$value.' length is '.$len.' not 6 !');
            exit;
        }
        if(!preg_match("/[0-9]{6}$/", $_POST[$value])){// is not right format
            echo EchoUtil::msgError($code, 'param error : '.$value.' format is not right !');
            exit;
        }
        return $_POST[$value];
    }
     
}
